﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HotelResSystem2
{
    public class PaymentSystem
    {
        private static PaymentSystem PaySysSingletonInstance;
        static readonly object padlock = new object();
        private PaymentSystem()
        {
        }

        public static PaymentSystem GetInstance()
        {
            lock (padlock)
            {
                if (PaySysSingletonInstance == null)
                {
                    PaySysSingletonInstance = new PaymentSystem();
                }
                return PaySysSingletonInstance;
            }
        }

        public double CalculateSubtotal(int days, double nightlyRate, Discount discount)
        {
            double subtotal = 0.0;

            // Price per night times days stayed
            subtotal = nightlyRate * days;

            // Apply a discount if one was set
            if (discount != null)
            {
                subtotal = subtotal * (1.0 - discount.Percentage);
            }

            return subtotal;
        }

        public double CalculateGrandTotal(double subtotal, double additionalCharges)
        {
            return subtotal + additionalCharges;
        }

        public bool GetPayment(Reservation res, Guest gst)
        {
            // check if correct guest
            if (res.PrimaryGuest != gst)
                return false;

            // get guest credit payment information
            GuestContactInfo GuestInfo = gst.ContactInfo;
            int GuestCreditInfo = GuestInfo.CreditInfo;

            AuthorizePayment(GuestCreditInfo);
            
            return true;
        }

        // may need to change creditInfo to something more "real", using int for easiness for now.
        public void AuthorizePayment(int creditInfo)
        {
            ServicesFactory factory = ServicesFactory.GetInstance();

            CreditInstitutionAdapter creditAdapter = factory.getCreditAdapter();

            bool isAuthorized = creditAdapter.AuthorizePayment(creditInfo);

            if (isAuthorized)
            {
                this.printInvoice();
            }
            else
            {
                // log error?
            }
        }

        public void printInvoice()
        {
            // electronically get guest's signature...???

        }

        public Bill PrepareBill(Reservation res, double additionalCharges)
        {
            Bill bill = null;
            if (res != null)
            {
                bill = new Bill();
                bill.Reservation = res;
                bill.AdditionalCharges = additionalCharges;
            }
            return bill;
        }
    }
}
